package com.slz3.business.generator;

import com.slz3.business.frame.util.HumpToUnderline;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class CloneTemplateGeneratorJava {

    static final Path SOURCE = Paths.get(
            "/Users/jinxing/SD/Projects/mishang-business-webflux/src/main/java/com/slz3/business/module/templateExample/temStyle1");
    public static String TARGET_CHILD_DIC = "/goodsManagement/gooBrandRecommendGoods";
    static final Path TARGET = Paths.get(
            "/Users/jinxing/SD/Projects/mishang-business-webflux/src/main/java/com/slz3/business/module/" + TARGET_CHILD_DIC);

    public static void main(String[] args) throws Exception {
        new CloneTemplateGeneratorJava().execute();
//    new CloneTemplateGenerator().replaceTarToSou("");

    }

    private String replaceTarToSou(String line) {
        String[] split = TARGET_CHILD_DIC.split("/");
        String first = split[1];
        String second = split[2];
        System.out.println(first);
        System.out.println(second);
        String firstCharUpper = String.valueOf(first.charAt(0)).toUpperCase() + first.substring(1);
        String secondCharUpper = String.valueOf(second.charAt(0)).toUpperCase() + second.substring(1);
        System.out.println(firstCharUpper);
        System.out.println(secondCharUpper);


        return line.replace("TemplateExample", firstCharUpper)
                .replace("templateExample", first)
                .replace("template_example", HumpToUnderline.convert(first))
                .replace("template-example", HumpToUnderline.convert(first).replace("_", "-"))

                .replace("TemStyle1", secondCharUpper)
                .replace("temStyle1", second)
                .replace("tem_style1", HumpToUnderline.convert(second))
                .replace("tem-style1", HumpToUnderline.convert(second).replace("_", "-"))
                ;
    }

    public boolean execute() throws Exception {
        // 1 遍历“模板根目录”下的每一个模板文件
        Files.walkFileTree(SOURCE, new CloneTemplateToTargetFileVisitor());
        return true;
    }

    /**
     * Created by jinxing on 2017/4/9. // 1 遍历“模板根目录”下的每一个模板文件 Files.walkFileTree(file, new
     * CloneTemplateToTargetFileVisitor());
     */
    class CloneTemplateToTargetFileVisitor extends SimpleFileVisitor<Path> {

        /**
         * 2.唯一要做的就是重写 visitFile，让它来为你工作；
         **/
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            // 3.过滤所有来源目录文件
            if (file.toString().endsWith(".java")
                    || file.toString().endsWith(".ts")
                    || file.toString().endsWith(".html")
                    || file.toString().endsWith(".proto")
                    || file.toString().endsWith(".jsp")
                    || file.toString().endsWith(".js")
                    || file.toString().endsWith(".css")) {
                // 来源目录文件绝对路径
                String souFileAbsDir = file.toString();
                System.out.println(souFileAbsDir);
                // 目标目录文件绝对路径 // 修改跟路径 // 修改文件名
                String tarFileAbsDir = replaceTarToSou(
                        souFileAbsDir.replace(SOURCE.toString(), TARGET.toString()));
                System.out.println(tarFileAbsDir);
                // [生成]目标目录 & 文件
                Path tarFolder = Paths
                        .get(tarFileAbsDir.substring(0, tarFileAbsDir.lastIndexOf(File.separator)));
                Path tarFile = Paths.get(tarFileAbsDir);
                if (Files.notExists(tarFolder)) {
                    Files.createDirectories(tarFolder);
                }
                if (Files.notExists(tarFile)) {
                    Files.createFile(tarFile);
                    // 写入
                    try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8);
                         BufferedWriter writer = Files.newBufferedWriter(tarFile, StandardCharsets.UTF_8,
                                 StandardOpenOption.WRITE)) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            writer.write(replaceTarToSou(line));
                            writer.write("\n");
                        }
                    }
                }
            }
            // 4.return
            return FileVisitResult.CONTINUE;
        }
    }

}
